package jp.co.jmas.jmasvideo.validator;

import java.util.List;

import jp.co.jmas.jmasvideo.dao.ErrorMessage;

class ValidatorHelper {
	static void validateString(List<ErrorMessage> messageList, String id, String value, int minLength, int maxLength,
			String pattern, String messageEmpty, String messageMaxLength, String messageMinLength, String messagePattern) {
		// Empty check
		if (value == null || value.isEmpty()) {
			if (messageEmpty != null) {
				// Error due to not specified
				messageList.add(new ErrorMessage(id, messageEmpty));
			}
			return;
		}

		// Length check
		if (value.length() < minLength && messageMinLength != null) {
			messageList.add(new ErrorMessage(id, messageMinLength));
			return;
		}

		if (value.length() > maxLength && messageMaxLength != null) {
			// Error due to not specified
			messageList.add(new ErrorMessage(id, messageMaxLength));
			return;
		}

		// Pattern check
		if (pattern != null && !value.matches(pattern)) {
			if (messagePattern != null) {
				// Error due to not specified
				messageList.add(new ErrorMessage(id, messagePattern));
				return;
			}
		}
	}
}
